import { request, encapsulation } from '@/network/request.js'
import { getToken } from '@/network/bookManager.js'

/*
 *@description: 分页查询书籍分类
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-20 14:44:13
 */
export async function queryClassificationPage(option) {
  let token = await getToken()
  return await request({
    url: `/api/tcb/invokecloudfunction?&access_token=${token.access_token}&env=zhuma-mbnti&name=groupTotalClassificationOrBook`,
    method: 'post',
    data: JSON.stringify(option)
  })
}
/*
 *@description: 查询分页总数
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-20 14:53:17
 */
export function queryClassificationTotal(option) {
  return encapsulation({
    url: '/api/tcb/databasecount',
    method: 'post',
    data: {
      query: `db.collection("boxBookClassification")
            .where({
                ${
                  option.search
                    ? `classificationName:db.RegExp({
                    regexp: '${option.search}',
                    options: 'i',
                  }),`
                    : ``
                }
                  classificationStatus :${option.status}
            }).count()`
    }
  })
}
/*
 *@description: 根据id修改 分类
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-20 22:04:41
 */
export function modifyClassificationById(option) {
  return encapsulation({
    url: '/api/tcb/databaseupdate',
    method: 'post',
    data: {
      query: `db.collection("boxBookClassification")
            .where({
                  _id :'${option._id}'
            }).update({
                data: {
                ${
                  option.classificationName
                    ? `classificationName:'${option.classificationName}',`
                    : ``
                }
                ${
                  option.classificationStatus!=undefined
                    ? `classificationStatus:${option.classificationStatus},`
                    : ``
                }
                }
            })`
    }
  })
}
/*
 *@description: 添加书籍分类
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-20 22:54:55
*/
export function addClassification(option) {
  return encapsulation({
    url: '/api/tcb/databaseadd',
    method: 'post',
    data: {
      query: `db.collection('boxBookClassification')
            .add({
              data: [
                {
                    classificationName: '${option.classificationName}',
                    classificationStatus: ${option.classificationStatus}
                }
              ]
            })
          `
    }
  })
}
/*
 *@functionName:  根据分类名称查询
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-08-26 16:26:34
 *@version: V1.0.5
*/
export function queryClassification(option) {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query: `db.collection('boxBookClassification')
      .where({
        classificationName: '${option}'
      })
      .get()`
    }
  })
}
/*
 *@functionName: 真正删除分类
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-01 19:57:22
 *@version: V1.0.5
*/
export function removeChapterByBookId(option) {
  return encapsulation({
    url: `/api/tcb/databasedelete`,
    method: 'post',
    data: {
      query: `db.collection('boxBookClassification')
      .where({
         _id:'${option}'
      }).remove()`
    }
  })
}

